//
//  SPDataCPFHeaderFooterView.m
//  panzer
//
//  Created by 还带大道 on 2018/10/24.
//  Copyright © 2018年 还带大道. All rights reserved.
//

#import "SPDataCPFHeaderFooterView.h"
#import "SPLiveDetailAreaCollectionViewCell.h"
#import "SPDataCPFSelectView.h"
#import "SPDataCModel.h"

@interface SPDataCPFHeaderFooterView()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;


@end

@implementation SPDataCPFHeaderFooterView


- (void)awakeFromNib
{
    [super awakeFromNib];
    
    [self setupShadow];
    _collectionView.backgroundView.backgroundColor = [UIColor whiteColor];
    _bgView.backgroundColor = [UIColor whiteColor];
    
    UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
    //设置headerView的尺寸大小
    flowLayout.headerReferenceSize = CGSizeMake(RZ_SCREEN_WIDTH, 140);
    flowLayout.footerReferenceSize =CGSizeZero;
    
    CGFloat width = 60;
    CGFloat height = 30;
    flowLayout.itemSize = CGSizeMake(width, height);
    // 设置最小行间距
    flowLayout.minimumLineSpacing = 15;
    // 设置最小列间距
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(15, 0, 15, 0);
    
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([SPLiveDetailAreaCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([SPLiveDetailAreaCollectionViewCell class])];
    
    //注册headerView  此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致  均为reusableView
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView_pf"];
    
}



+ (instancetype)shareHeaderView
{
    return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil].lastObject;
}


- (void)setupShadow
{
    _bgView.layer.shadowColor = RZ_COLOR_ALPHA(201, 201, 201, 0.37).CGColor;
    _bgView.layer.cornerRadius = 5;
    _bgView.layer.shadowOffset = CGSizeMake(0,1);//shadowOffset阴影偏移,x向右偏移，y向下偏移，默认(0, -3)
    _bgView.layer.shadowOpacity = 1;//阴影透明度，默认0
    _bgView.layer.shadowRadius = 4;//阴影半径，默认3
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    [self.collectionView layoutIfNeeded];
    UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
    CGSize size = flowLayout.itemSize;
    CGFloat width = self.collectionView.width / 4;
    size.width = width;
    flowLayout.itemSize = size;
    
    [self refresh];
}



- (void)setModel:(SPDataCModel *)model
{
    _model = model;
    [self.collectionView reloadData];
}


#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSInteger count = 0;
    switch (_genderType) {
        case GenderType_ALL:
            count = self.all.count;
            break;
        case GenderType_WOMAN:
            count = self.woman.count;
            break;
        case GenderType_MAN:
            count = self.man.count;
            break;
    }
    return count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    SPLiveDetailAreaCollectionViewCell *cell =  [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([SPLiveDetailAreaCollectionViewCell class]) forIndexPath:indexPath];
    cell.title.textColor = RZ_COLOR(155, 155, 155);
    cell.title.font = [UIFont systemFontOfSize:10];
    cell.count.textColor = RZ_COLOR(74, 74, 74);
    cell.count.font = [UIFont systemFontOfSize:12];
    cell.unit.hidden = YES;
    cell.count_centerX_offset.constant = 0;
    
    switch (_genderType) {
        case GenderType_ALL:
            cell.title.text = _all[indexPath.item];
            break;
        case GenderType_WOMAN:
            cell.title.text = _woman[indexPath.item];
            break;
        case GenderType_MAN:
            cell.title.text = _man[indexPath.item];
            break;
    }
    cell.count.text = @"80000";
    return cell;
}



//通过设置SupplementaryViewOfKind 来设置头部或者底部的view，其中 ReuseIdentifier 的值必须和 注册是填写的一致，本例都为 “reusableView”
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView_pf" forIndexPath:indexPath];
    headerView.backgroundColor =[UIColor clearColor];
    
    SPDataCPFSelectView *view = [SPDataCPFSelectView shareHeaderView];
    view.backgroundColor = [UIColor whiteColor];
    view.frame = headerView.bounds;
    view.genderSwitchBlock = ^(GenderType type) {//切换性别
        if(type != self.genderType){
            self.genderType = type;
        }
    };
    view.type = _genderType;
    view.model = _model;
    [headerView addSubview:view];
    return headerView;
}



- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    RZLog(@"collectionView--%ld",(long)indexPath.item);
}


#pragma mark--setter&&getter

- (void)setGenderType:(GenderType)genderType
{
    _genderType = genderType;
    [self.collectionView reloadData];
    [self refresh];
}


- (void)refresh
{
    UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
    CGFloat height = flowLayout.collectionViewContentSize.height+10;
    !self.refreshBlock?:self.refreshBlock(_genderType,height);
}

@end
